//
//  UIAlertMaker.m
//
//  Created by 慧趣小歪 on 16/12/6.
//  Copyright © 2016年 小分队. All rights reserved.
//

#import "UIAlertMaker.h"



@interface UIAlertMaker ()

@property (nonatomic, readwrite) UIAlertControllerStyle mStyle;
@property (nonatomic, readwrite) NSString *mTitle;
@property (nonatomic, readwrite) NSString *mMessage;
@property (nonatomic, readwrite) NSMutableArray *actions;
@property (nonatomic, readwrite) NSString *actionTitle;
@property (nonatomic, readwrite) UIAlertActionStyle actionStyle;
@property (nonatomic, readwrite) SEL actionSeletor;
@property (nonatomic, readwrite) id actionTag;
@property (nonatomic, readwrite) UIViewController *controller;

- (void)makeAction;

@end

UIAlertMaker * UIAlert(UIAlertControllerStyle style) {
    UIAlertMaker *maker = [[UIAlertMaker alloc] init];
    maker.mStyle = style;
    return maker;
}

@implementation UIAlertMaker


// 根据当前按钮标题生成事件
- (void)makeAction {
    if (_actions == nil) _actions = [NSMutableArray array];
    if (_actionTitle == nil) return;
    if (!_actionSeletor) {
        UIAlertActionStyle style = _actionStyle == 0 ? UIAlertActionStyleCancel : _actionStyle;
        [_actions addObject:[UIAlertAction actionWithTitle:_actionTitle style:style handler:nil]];
    } else {
        __block SEL selector = _actionSeletor;
        __block id tag = _actionTag;
        __block UIAlertMaker *maker = self;
        [_actions addObject:[UIAlertAction actionWithTitle:_actionTitle style:_actionStyle handler:^(UIAlertAction * _Nonnull action) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [maker.controller performSelector:selector withObject:tag ?: action];
            maker = nil;
#pragma clang diagnostic pop
        }]];
    }
    _actionTitle = nil;
    _actionSeletor = nil;
    _actionStyle = UIAlertActionStyleDefault;
}

- (ALERT_TAG)byParam {
    return ^(id tag) {
        _actionTag = tag;
        return self;
    };
}

- (ALERT_TEXT)title {
    return ^(NSString * text) {
        _mTitle = text;
        return self;
    };
}

- (ALERT_TEXT)message {
    return ^(NSString * text) {
        _mMessage = text;
        return self;
    };
}

- (ALERT_TEXT)button {
    [self makeAction];
    return ^(NSString * text) {
        _actionTitle = text;
        return self;
    };
}

- (ALERT_STYLE)style {
    return ^(UIAlertActionStyle style) {
        _actionStyle = style;
        return self;
    };
}

- (ALERT_ACTION)onTappedCall {
    return ^(SEL action) {
        _actionSeletor = action;
        return self;
    };
}

- (void)presentWith:(UIViewController *)controller; {
    if (controller == nil) return;
    _controller = controller;
    [self makeAction];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:_mTitle message:_mMessage preferredStyle:_mStyle];
    for (UIAlertAction *action in _actions) {
        [alert addAction:action];
    }
    [controller presentViewController:alert animated:YES completion:nil];
    //[controller showDetailViewController:alert sender:controller];
}

@end

